var pfund = {
	base: "",
	locale: "zh_CN"
};

//(function($) {
//	// 检测登录
//	$.checkLogin = function() {
//		var result = false;
//		$.ajax({
//			url: pfund.base + "/login/check.shtml",
//			type: "GET",
//			dataType: "json",
//			cache: false,
//			async: false,
//			success: function(data) {
//				result = data;
//			}
//		});
//		return result;
//	}
//
//	// 跳转登录
//	$.redirectLogin = function (redirectUrl, message) {
//		var href = pfund.base + "/login.shtml";
//		if (redirectUrl != null) {
//			href += "?redirectUrl=" + encodeURIComponent(redirectUrl);
//		}
//		if (message != null) {
//			alert(message);
//			setTimeout(function() {
//				location.href = href;
//			}, 1000);
//		} else {
//			location.href = href;
//		}
//	}
//})(jQuery);

var basePath = "";
$().ready(function(){
	basePath = $("#basePath").val()
	if ($.tools != null) {
		var $tab = $("#tab");
		// Tab效果
		$tab.tabs("table.tabContent, div.tabContent");
	}
	
	loadKindEditor();
});

function loadKindEditor() {
	// 编辑器
	if(typeof(KindEditor) != "undefined") {
		KindEditor.ready(function(K) {
			editor = K.create(".editor", {
				items: [
					"source", "|", "undo", "redo", "|", "preview", "template", "cut", "copy", "paste",
					"plainpaste", "wordpaste", "|", "justifyleft", "justifycenter", "justifyright",
					"justifyfull", "insertorderedlist", "insertunorderedlist", "indent", "outdent", "subscript",
					"superscript", "clearhtml", "quickformat", "selectall", "|", "fullscreen", "/",
					"formatblock", "fontname", "fontsize", "|", "forecolor", "hilitecolor", "bold",
					"italic", "underline", "strikethrough", "lineheight", "removeformat", "|", "image",
					"flash", "media", "table", "hr", "emoticons", "baidumap", "pagebreak",
					"anchor", "link", "unlink"
				],
				//autoHeightMode : true,
				filterMode : false,
				minHeight: 200,
				uploadJson : basePath+"kindeditor/upload.shtml",
				afterCreate : function() {
					//this.loadPlugin('autoheight');
				},
				afterChange: function() {
					this.sync();
				}
			});
		});
	}
}

//添加Cookie
function addCookie(name, value, options) {
	if (arguments.length > 1 && name != null) {
		if (options == null) {
			options = {};
		}
		if (value == null) {
			options.expires = -1;
		}
		if (typeof options.expires == "number") {
			var time = options.expires;
			var expires = options.expires = new Date();
			expires.setTime(expires.getTime() + time * 1000);
		}
		document.cookie = encodeURIComponent(String(name)) + "=" + encodeURIComponent(String(value)) + (options.expires ? "; expires=" + options.expires.toUTCString() : "") + (options.path ? "; path=" + options.path : "") + (options.domain ? "; domain=" + options.domain : ""), (options.secure ? "; secure" : "");
	}
}

//获取Cookie
function getCookie(name) {
	if (name != null) {
		var value = new RegExp("(?:^|; )" + encodeURIComponent(String(name)) + "=([^;]*)").exec(document.cookie);
		return value ? decodeURIComponent(value[1]) : null;
	}
}

//移除Cookie
function removeCookie(name, options) {
	addCookie(name, null, options);
}

/*
图片上传预览 IE是用了滤镜
<input type="file" onchange="previewImage(this)" />
<div id="preview"></div>
*/
function previewImage(file, previewId, index) {
	var MAXWIDTH = 100;
	var MAXHEIGHT = 100;
	var div = (previewId == null || previewId == "") ? document.getElementById('preview') : document.getElementById(previewId);
	var index = (index == null || index == "") ? 1:index;
	if (file.files && file.files[0]) {
		div.innerHTML = '<img id=imghead'+index+'>';
		var img = document.getElementById('imghead'+index);
		img.onload = function() {
			var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth,
					img.offsetHeight);
			//img.width = rect.width;
			//img.height = rect.height;
			// img.style.marginLeft = rect.left+'px';
			img.style.marginTop = rect.top + 'px';
		}
		var reader = new FileReader();
		reader.onload = function(evt) {
			img.src = evt.target.result;
		}
		reader.readAsDataURL(file.files[0]);
	} else // 兼容IE
	{
		var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
		file.select();
		var src = document.selection.createRange().text;
		div.innerHTML = '<img id=imghead'+index+'>';
		var img = document.getElementById('imghead'+index);
		img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
		var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth,
				img.offsetHeight);
		status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
		/*
		div.innerHTML = "<div id=divhead style='width:" + rect.width
				+ "px;height:" + rect.height + "px;margin-top:" + rect.top
				+ "px;" + sFilter + src + "\"'></div>";
		*/
		div.innerHTML = "<div id=divhead style='margin-top:" + rect.top
			+ "px;" + sFilter + src + "\"'></div>";
	}
}
function clacImgZoomParam(maxWidth, maxHeight, width, height) {
	var param = {
		top : 0,
		left : 0,
		width : width,
		height : height
	};
	if (width > maxWidth || height > maxHeight) {
		rateWidth = width / maxWidth;
		rateHeight = height / maxHeight;

		if (rateWidth > rateHeight) {
			param.width = maxWidth;
			param.height = Math.round(height / rateWidth);
		} else {
			param.width = Math.round(width / rateHeight);
			param.height = maxHeight;
		}
	}

	param.left = Math.round((maxWidth - param.width) / 2);
	param.top = Math.round((maxHeight - param.height) / 2);
	return param;
}